package com.fanrui.code.script;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.fanrui.code.config.ConfigFileManager;
import com.fanrui.code.entity.MapCondition;
import com.fanrui.code.entity.PlayerCondition;
import com.fanrui.code.player.Player;
import com.fanrui.proto.cmd.CurrencyType;

public class ConditionManager {

	// singleton
	private ConditionManager() {
	}

	private static class ConditionManagerSingleton {
		private static ConditionManager instance = new ConditionManager();
	}

	public static ConditionManager getInstance() {
		return ConditionManagerSingleton.instance;
	}

	public PlayerCondition getConditionById(int id) {
		return ConfigFileManager.getInstance().getConfigFileById(PlayerCondition.class, id);
	}

	public static boolean checkPlayerCondition(Player player, int conditionId) {
		return checkPlayerCondition(player, ConditionManager.getInstance().getConditionById(conditionId));
	}
	public static boolean checkPlayerCondition(Player player,String jsonStr){
    	Gson gosn = new Gson();
    	PlayerCondition condition = gosn.fromJson(jsonStr, PlayerCondition.class);
    	return checkPlayerCondition(player,condition);
    }
	public static boolean checkPlayerCondition(Player player, PlayerCondition condition) {

		if(player == null){
    		return false;
    	}
		if(condition !=null ){
			if(condition.getGoldGe() != null && player.getCurrencyValue(CurrencyType.C_Gold) < condition.getGoldGe() ){
	    		return false;
			}
			if(condition.getMoneyGe() != null && player.getCurrencyValue(CurrencyType.C_Money) < condition.getMoneyGe() ){
	    		return false;
			}
//			if(condition.getHaveItems() != null){
//				JsonArray jsonArray = new JsonParser().parse(condition.getHaveItems()).getAsJsonArray();
//				if(jsonArray != null && jsonArray.size() > 0){
//					for(JsonElement jsonElement:jsonArray){
//						if(jsonElement.isJsonPrimitive()){
//							int num = player.getItemNum(jsonElement.getAsInt());
//							if(num <= 0){
//								return false;
//							}
//						}
//					}
//				}
//			}
			
    	}
    	return true;
	}

	public static boolean checkMapCondition(Player player, MapCondition condition) {
		return true;
	}

	/**
	 * 获取未能满足条件的字符串
	 */
	public static String getConditionStr(Player player, PlayerCondition condition) {
		if (condition != null) {
			if (condition.getInFamily() != null) { // 是否在某个家族中

			}
			if (condition.getInRoom() != null) { // 是否在某个房间中

			}
			if (condition.getGoldGe() != null && condition.getGoldGe() > 0) { // 金币大于等于

			}
			if (condition.getGoldLe() != null && condition.getGoldLe() > 0) { // 金币小于等于

			}
			if (condition.getMoneyGe() != null && condition.getMoneyGe() > 0) { // 砖石大于等于

			}
			if (condition.getMoneyLe() != null && condition.getMoneyLe() > 0) { // 砖石小于等于

			}
			if (condition.getVipGe() != null && condition.getVipGe() > 0) { // Vip大于等于

			}
			if (condition.getVipLe() != null && condition.getVipLe() > 0) { // Vip小于等于

			}
			if (condition.getHaveItems() != null && StringUtils.isNoneBlank(condition.getHaveItems())) { // Vip小于等于

				@SuppressWarnings("unused")
				JsonArray jsonArray = new JsonParser().parse(condition.getHaveItems()).getAsJsonArray();
//				if (jsonArray != null && jsonArray.size() > 0) {
//					for (JsonElement element : jsonArray) {
//						if (element.isJsonPrimitive()) {
//							ItemBag itemBag = player.getItemBagById(element.getAsInt());
//							if (itemBag == null || itemBag.getNum() <= 0) {
//								return "缺少物品：" + element.getAsInt();
//							}
//						}
//					}
//				}
			}
		}
		return "";
	}

	/**
	 * 获取未能满足条件的字符串
	 */
	public static String getConditionStr(Player player, int conditionId) {
		return getConditionStr(player, ConditionManager.getInstance().getConditionById(conditionId));
	}
}
